﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project
{
    class SVProjects
    {
        private static string query;

        /*
         * The following function shows all of the projects of
         * a single supervisor with the given ID.
         */
        public static DataTable showMyProject(string id)
        {
            DataTable dt = new DataTable();
            query = "SELECT * FROM tbl_Projects WHERE fld_supervisorID Like '" + id + "'";
            try
            {
                dt = AdoHelper.ExecuteDataTable(query);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            return dt;
        }

        /*
         * The following function gets a query which searches a
         * log and returns a datatable with the values from the
         * database.
         */
        public static DataTable searchLog(string searchQuery)
        {
            DataTable dt = new DataTable();
            try
            {
                dt = AdoHelper.ExecuteDataTable(searchQuery);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            return dt;
        }

        public static DataTable searchEmployees(string projectID)
        {
            DataTable dt = new DataTable();
            query = "SELECT * FROM tbl_ProjectsOfEmployees WHERE fld_projectID Like '" + projectID + "'";
            dt = AdoHelper.ExecuteDataTable(query);
            DataRow[] dr;
            StringBuilder eQuery=new StringBuilder();
            string start = "SELECT * FROM tbl_Employees WHERE";
            eQuery.Append(start);
            dr = dt.Select();
            for (int i = 0; i < dr.Length; i++)
            {
                eQuery.Append(" AND fld_employeeID Like '" + dr[0].Field<string>("fld_employeeID") + "'");
            }
            try
            {
                eQuery.Remove(start.Length + 1, 4);
                dt= AdoHelper.ExecuteDataTable(eQuery.ToString());
            }
            catch
            {
                MessageBox.Show("No employees for this project", "Error");
            }
            return dt;
        }

        /*
         * The following function adds a task for a project to an
         * employee for a project that the logged in supervisor is
         * in charge of.
         */
        public static void addTask(string employeeID, string projectID, string task, string deadline)
        {
            DateTime currentDate = DateTime.Today;
            query = "INSERT INTO tbl_Logs(fld_employeeID, fld_projectID, fld_task, fld_givenDate, fld_deadline) VALUES('" + employeeID + "', '" + projectID + "', '" + task + "', '" + currentDate.ToString().Substring(0, 10) + "', '" + deadline + "')";
            AdoHelper.ExecuteNonQuery(query);
        }
    }
}
